package com.softer.wxzj.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.softer.wxzj.common.entity.CommonResult;
import com.softer.wxzj.common.entity.Page;
import com.softer.wxzj.common.enums.NumStrEnum;
import com.softer.wxzj.common.enums.ShiroResultEnum;
import com.softer.wxzj.common.error.CourseException;
import com.softer.wxzj.common.util.StringUtils;
import com.softer.wxzj.entity.ExpProjectReq;
import com.softer.wxzj.entity.OwnerSignature;
import com.softer.wxzj.mapper.ExpProjectReqMapper;
import com.softer.wxzj.mapper.OwnerSignatureMapper;
import com.softer.wxzj.service.OwnerSignatureService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class OwnerSignatureServiceImpl implements OwnerSignatureService {
    @Autowired
    private OwnerSignatureMapper ownerSignatureMapper;
    @Autowired
    private  ExpProjectReqMapper expProjectReqMapper;
    /**
     * 获取业主签字明细
     *
     * @param ownerSignature 业主实体
     * @return 业主分摊明细
     */
    @Override
    public PageInfo<OwnerSignature> selectByOwnerSignatureInfo(Page page, OwnerSignature ownerSignature) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        List<OwnerSignature> list;
        if (StringUtils.isEmpty(ownerSignature.getFuzzy())) {
            list = ownerSignatureMapper.selectByOwnerSignatureInfo(ownerSignature);
        } else {
            list = ownerSignatureMapper.selectByOwnerSignatureInfoFuzzy(ownerSignature.getReqId(), ownerSignature.getFuzzy());
        }
        PageInfo<OwnerSignature> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    /**
     * 业主签字（是否同意）
     *
     * @return 结果
     */
    @Override
    public CommonResult updateByPrimaryKeySelective(List<String> idList, String opinion) {
      if (idList.isEmpty()){
          throw new CourseException(ShiroResultEnum.FAIL.getCode(), "请选择数据");
      }
        OwnerSignature ownerSignature = new OwnerSignature();
        ownerSignature.setOpinion(opinion);
        ownerSignature.setIdList(idList);
        //改状态
        int zoneCodeCount = ownerSignatureMapper.updateOwnerSignature(ownerSignature);
        return CommonResult.ResponseResult(zoneCodeCount);
    }

    /**
     * 业主信息汇总
     *
     * @param reqId 项目id
     * @return 结果
     */
    @Override
    public OwnerSignature getSum(String reqId) {
        OwnerSignature o = ownerSignatureMapper.getSum(reqId);
        ExpProjectReq expProjectReq = expProjectReqMapper.getInfoById(reqId);
        o.setFlag(true);
        if(NumStrEnum.ONE.getNum().equals(expProjectReq.getFlowState())||NumStrEnum.TWO.getNum().equals(expProjectReq.getFlowState())){
            o.setFlag(false);
        }
        return o;
    }
}
